package com.ruanda.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruanda.domain.Category;
import com.ruanda.model.Result;
import com.ruanda.service.CategoryService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 商品类目控制层
 */
@Tag(name = "商品类目接口管理")
@RequestMapping("prod/category")
@RestController
public class CategoryController {
    @Autowired
    private CategoryService categoryService;
    

    @Operation(summary = "查询商品所有类目")
    @GetMapping("table")
    @PreAuthorize("hasAuthority('prod:category:page')")
    public Result<List<Category>> loadCategoryList() {
        List<Category> categories = categoryService.queryAllCategoryList();
        return Result.success(categories);
    }

    @Operation(summary = "查询商品一级类目")
    @GetMapping("listCategory")
    @PreAuthorize("hasAuthority('prod:category:page')")
    public Result<List<Category>> loadRootCategoryList() {
        List<Category> rootCategoryList = categoryService.queryRootCategoryList();
        return Result.success(rootCategoryList);
    }

    @Operation(summary = "新增商品类目")
    @PostMapping
    @PreAuthorize("hasAuthority('prod:category:save')")
    public Result<String> saveCategory(@RequestBody Category category) {
        Boolean count = categoryService.saveCategory(category);
        return Result.handle(count);
    }

    @Operation(summary = "查询商品类目详情")
    @GetMapping("info/{categoryId}")
    @PreAuthorize("hasAuthority('prod:category:info')")
    public Result<Category> loadCategoryInfo(@PathVariable Long categoryId) {
        Category category = categoryService.getById(categoryId);
        return Result.success(category);
    }

    @Operation(summary = "修改商品类目")
    @PutMapping
    @PreAuthorize("hasAuthority('prod:category:update')")
    public Result<String> modifyCategory(@RequestBody Category category) {
        Integer count = categoryService.modifyCategory(category);
        return Result.handle(count>0);
    }

    @Operation(summary = "删除商品类目")
    @DeleteMapping("{categoryId}")
    @PreAuthorize("hasAuthority('prod:category:delete')")
    public Result<String> removeCategory(@PathVariable Long categoryId) {
        Integer count = categoryService.removeCategoryById(categoryId);
        return Result.handle(count>0);
    }
    
    ////////////  小程序  ////////////
    @Operation(summary = "查询小程序商品的一级类目集合")
    @GetMapping("category/list")
    public Result<List<Category>> loadRootCategoryListForApp(Long parentId) {
        List<Category> rootCategoryList = categoryService.queryWxCategoryListByPid(parentId);
        return Result.success(rootCategoryList);
    }

    @GetMapping("getCateqoryListByParentId")
    public Result<List<Category>> getCategoryListByParentId(@RequestParam Long parentId){
        List<Category> categories = categoryService.list(new LambdaQueryWrapper<Category>()
                    .eq(Category::getParentId, parentId)
        );
        return Result.success(categories);
    }
}
